package com.ruoyi.experiment.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.experiment.domain.ExperimentDesign;
import com.ruoyi.experiment.service.IExperimentDesignService;
import com.ruoyi.system.api.RemoteAnimalInfomationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.experiment.domain.ExperimentRecord;
import com.ruoyi.experiment.service.IExperimentRecordService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 过程记录Controller
 * 
 * @author gw
 * @date 2025-01-08
 */
@RestController
@RequestMapping("/record")
public class ExperimentRecordController extends BaseController
{
    @Autowired
    private IExperimentRecordService experimentRecordService;

    @Autowired
    private IExperimentDesignService experimentDesignService;

    @Autowired
    private RemoteAnimalInfomationService remoteAnimalInfomationService;

    /**
     * 查询过程记录列表
     */
    @RequiresPermissions("Experiment:record:list")
    @GetMapping("/list")
    public TableDataInfo list(ExperimentRecord experimentRecord)
    {
        startPage();
        List<ExperimentRecord> list = experimentRecordService.selectExperimentRecordList(experimentRecord);
        for (ExperimentRecord record : list) {
            ExperimentDesign experimentDesign = experimentDesignService.selectExperimentDesignByDesignId(record.getDesignId());
            AjaxResult ajaxResult = remoteAnimalInfomationService.getInfo(record.getAnimalId());
            Map map = (Map)ajaxResult.get("data");
            record.setAnimalNumber(map.get("animalNumber").toString());
            record.setDesignName(experimentDesign.getDesignName());
        }
        return getDataTable(list);
    }

    /**
     * 导出过程记录列表
     */
    @RequiresPermissions("Experiment:record:export")
    @Log(title = "过程记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExperimentRecord experimentRecord)
    {
        List<ExperimentRecord> list = experimentRecordService.selectExperimentRecordList(experimentRecord);
        ExcelUtil<ExperimentRecord> util = new ExcelUtil<ExperimentRecord>(ExperimentRecord.class);
        util.exportExcel(response, list, "过程记录数据");
    }

    /**
     * 获取过程记录详细信息
     */
    @RequiresPermissions("Experiment:record:query")
    @GetMapping(value = "/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId)
    {
        return success(experimentRecordService.selectExperimentRecordByRecordId(recordId));
    }

    /**
     * 新增过程记录
     */
    @RequiresPermissions("Experiment:record:add")
    @Log(title = "过程记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExperimentRecord experimentRecord)
    {
        return toAjax(experimentRecordService.insertExperimentRecord(experimentRecord));
    }

    /**
     * 修改过程记录
     */
    @RequiresPermissions("Experiment:record:edit")
    @Log(title = "过程记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExperimentRecord experimentRecord)
    {
        return toAjax(experimentRecordService.updateExperimentRecord(experimentRecord));
    }

    /**
     * 删除过程记录
     */
    @RequiresPermissions("Experiment:record:remove")
    @Log(title = "过程记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recordIds}")
    public AjaxResult remove(@PathVariable Long[] recordIds)
    {
        return toAjax(experimentRecordService.deleteExperimentRecordByRecordIds(recordIds));
    }
}
